import Vue from 'vue'
import VueRouter from 'vue-router'
import login from '../views/login'
import layout from '../views/layout'
import home from '../views/layout/home'
import querstion from '../views/layout/querstion'
import movie from '../views/layout/movie'
import user from '../views/layout/user'
import { getToken, delToken } from '../utils/token'
import { Toast } from 'vant'
import { getUserInfo } from '../Api/user'
import store from '../store/index'
import edit from '../views/layout/edit'
import article from '../views/layout/article'
import search from '../views/layout/search'
import result from '../views/layout/result'
import robot from '../views/layout/robot'

Vue.use(VueRouter)
const routes = [
  {
    path: '/login',
    name: 'login',
    component: login
  },
  {
    path: '/',
    redirect: 'home',
    name: 'layout',
    component: layout,
    children: [
      {
        path: 'home',
        name: 'home',
        component: home
      },
      {
        path: 'querstion',
        name: 'querstion',
        component: querstion
      },
      {
        path: 'movie',
        name: 'movie',
        component: movie
      },
      {
        path: 'user',
        name: 'user',
        component: user,
        meta: {
          needLogin: true
        }
      },
      {
        path: 'edit',
        name: 'edit',
        component: edit,
        meta: {
          needLogin: true,
          hideTabbar: true
        }
      },
      {
        path: 'article',
        name: 'article',
        component: article,
        meta: {
          hideTabbar: true
        }
      },
      {
        path: 'search',
        name: 'search',
        component: search,
        meta: {
          hideTabbar: true
        }
      },
      {
        path: 'result',
        name: 'result',
        component: result,
        meta: {
          hideTabbar: true
        }
      },
      {
        path: 'robot',
        name: 'robot',
        component: robot,
        meta: {
          hideTabbar: true
        }
      }
    ]
  }
]

// 吞掉 没有处理的 promise的错误
const originalPush = VueRouter.prototype.push

VueRouter.prototype.push = function push (location, onResolve, onReject) {
  if (onResolve || onReject) {
    // 执行之后 阻断后续代码
    return originalPush.call(this, location, onResolve, onReject)
  }
  return originalPush.call(this, location).catch(err => err)
}

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  // if (to.meta.needLogin !== true) {
  //   return next()
  // }

  const token = JSON.parse(getToken())

  if (token === null && to.meta.needLogin !== true) {
    return next()
  }

  if (token === null && to.meta.needLogin === true) {
    Toast.fail('请先登入')
    return next({ path: '/login', query: { redirect: to.path } })
  }

  if (store.state.userInfo.name !== undefined) {
    return next()
  }
  getUserInfo()
    .then(res => {
      store.commit('setUserInfo', res.data.data)
      return next()
    })
    .catch(err => {
      if (err.response.status === 401 || err.response.status === 400) {
        delToken()
        if (to.meta.needLogin === true) {
          Toast.fail('登入异常,请重新登入')
          delToken()
          return next({ path: '/login', query: { redirect: to.path } })
        }
        return next()
      }
    })
})

export default router
